package com.tinysand.advert.service.impl;

import com.tinysand.advert.entity.Advertisement;
import com.tinysand.advert.exception.AdvertException;
import com.tinysand.advert.repository.AdvertRepository;
import com.tinysand.advert.service.AdvertService;
import com.tinysand.advert.support.AdvertRequest;
import com.tinysand.advert.support.AdvertResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 广告服务接口实现
 *
 * Created By TINY
 * Create Time 2020/2/21
 **/
@Slf4j
@Service
public class AdvertServiceImpl implements AdvertService {

    /* 广告数据持久层对象 */
    private final AdvertRepository advertRepository;

    public AdvertServiceImpl(AdvertRepository advertRepository) {
        this.advertRepository = advertRepository;
    }

    /**
     * 创建广告
     *
     * @param request 广告创建请求对象 {@link AdvertRequest}
     * @return 广告请求响应对象 {@link AdvertResponse}
     * @throws AdvertException 业务异常
     */
    @Override
    public AdvertResponse createAdvertisement(AdvertRequest request)
            throws AdvertException {

        Advertisement advertisement = advertRepository.save(
                request.convertToEntity()
        );

        return new AdvertResponse(
                advertisement.getId(), advertisement.getName());
    }
}
